package de.fhd.pms.dao;

import org.hibernate.*;
import org.hibernate.cfg.*;

import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;

import de.fhd.pms.model.Action;
import de.fhd.pms.model.Tour;
import de.fhd.pms.model.User;
import de.fhd.pms.model.UsersTour;

/** The data access class for UsersTours. */
public class UsersTourDao extends HibernateDaoSupport {

	private UserDao userDao;
	private TourDao tourDao;
	private ActionDao actionDao;
	private Object session;

	private UsersTourDao usersTourDao;
	
	/**
	 * Set TourParticipant
	 * @param tourDao
	 * @param userDao
	 */
	public void setTourParticipant(TourDao tourDao, UserDao userDao) {
		this.tourDao = tourDao;
		this.userDao = userDao;
	}

	/**
	 * Set ActionDao
	 * @param actionDao
	 */
	public void setActionDao(ActionDao actionDao) {
		this.actionDao = actionDao;
	}

	/**
	 * Get Users Tour By Tour
	 * @param Tour tour
	 * @return List<UsersTour>
	 */
	public List<UsersTour> getUsersTourByTour(Tour tour) {
		HibernateTemplate template = getHibernateTemplate();
		// search UsersTour in the database by tour id
		List<UsersTour> find = template.find(
				"from UsersTour as ut where ut.tourId = ?", tour.getId());
		// set all found records to the temporal variable 
		List<UsersTour> tmp = find;
		// return temporal variable with found UsersTour records in object form
		return tmp;
	}
	
	/**Finds out if User is registered to Tour or not.
	 * @param tour
	 * @param user
	 * @return false or true
	  */
	public boolean isUserRegisteredToTour(Tour tour, User user) {
		// when method public UsersTour searchByUserAndTour(Tour tour, User user)
		// returned null
		// then it means that user specified as the parameter is not registered 
		// to the specified as the parameter tour
		if (searchByUserAndTour(tour, user) == null) {
			return false;
		}
		return true;
	}
	
	/**
	 * Saves a UserTour
	 * @param usersTour
	 * @return usersTour
	 */
	public UsersTour save(UsersTour usersTour) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(usersTour);
		return usersTour;
	}
	
	/**
	 * Search by User and Tour
	 * @param tour
	 * @param user
	 * @return find an instance of {@link UsersTour}
	 */
	public UsersTour searchByUserAndTour(Tour tour, User user) {
		HibernateTemplate template = getHibernateTemplate();
		// initialise resulting record as null value 
		UsersTour find = null;
		// user and tour id's from parameters
		Integer userId = user.getId();
		Integer tourId = tour.getId();
		
		// test printout
		// System.out.println(userId);
		// System.out.println(tourId);

		// search for UsersTour records in the data base by tour and user id's
		// and get search result as List of UsersTour objects
		List<UsersTour> find_list = template.find(
				"from UsersTour as ut where ut.tourId = ? AND ut.userId = ?",
				tour.getId(), user.getId());
		
		// if search result as List of UsersTour objects is null,
		// then there is no record with specified parameters
		// if search result as List of UsersTour objects contains more objects then one,
		// it means that database data is corrupted
		// if both situations are not met, then get first element
		// from List of UsersTour objects as a search result
		if (find_list != null && find_list.size() == 1) {
			find = find_list.get(0);
		}
		
		// return found UsersTour object
		return find;
	}
	
	/**
	 * Deletes a UsersTour
	 * @param usersTour
	 * @throws DaoException 
	 */
	public void delete(UsersTour usersTour) throws DaoException {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(usersTour);
	}

	/**
	 * Set UsersTourDao
	 * @param usersTourDao
	 */
	public void setUsersTourDao(UsersTourDao usersTourDao) {
		this.usersTourDao = usersTourDao;
	}
	
	/**
	 * Set UserDao
	 * @param userDao
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	/**
	 * Set TourDao
	 * @param tourDao
	 */
	public void setTourDao(TourDao tourDao) {
		this.tourDao = tourDao;
	}


}
